/**
 * Created with IntelliJ IDEA.
 * User: xi
 * Date: 13-12-5
 * Time: 下午3:22
 * To change this template use File | Settings | File Templates.
 */


$(document).ready(function () {
    //聚焦第一个输入框
    $("#name").focus();
    //为inputForm注册validate函数
    $("#inputForm").validate({
        rules: {
            name: "required",
            url: "required"
        }
    });
});
(function () {
    angular.module("api-app", []);
    angular.module('api-app', []).config(function ($sceProvider) {
        // Completely disable SCE to support IE7.
        $sceProvider.enabled(false);
    });
    window.AllDocCtrl = function ($scope, $rootScope) {

        $scope.loadList = function () {
            $.get("/security/apidoc/list", function (data) {
                $scope.apiDocList = data;
                if (!$rootScope.$$phase) {
                    $scope.$apply();
                }
            }, "json");
        }
        $scope.loadList();


        $scope.edit = function (api) {
            $scope.formData = api;
            $("#dataTextArea").blur();
            $("#codeDiv").html("");
            $scope.formData.isEdit = true;
        }

        $scope.addItem = function () {
            $scope.formData = {
                dataType: "html", method: "GET", data: "{}", isEdit: true
            }
            $("#dataTextArea").show();
            $("#codeDiv").html("");
            $("#dataTextAreaCode").html("");
        }
        $scope.addItem();

        $scope.view = function (api) {
            $scope.formData = api;
            $("#codeDiv").html("");
            $("#dataTextArea").blur();
            $scope.formData.isEdit = false;

        }

        $scope.removeItem = function (id) {
            if (confirm("确认删除该条API数据？")) {
                $scope.formData.isEdit = false;
                $.post("/security/apidoc/delete/" + id, function (data) {
                    $scope.loadList();
                    alert("操作成功！");
                });
            }
        }

        $scope.save = function () {
            if ($("#inputForm").valid()) {
                $scope.formData.result = null;
                delete $scope.formData.insertTime;
                delete $scope.formData.lastUpdateTime;
                $.post("/security/apidoc/save", $scope.formData, function (data) {
                    if (!$scope.formData.id) {
                        $scope.loadList();
                    }
                    alert("操作成功！");
                }, "json");
                //$scope.addItem();
            }
        }
        $scope.test = function () {
            if (!$("#inputForm").valid()) {
                return false;
            }
            $.ajax({
                url: $scope.formData.url,
                type: $scope.formData.method,
                data: JSON.parse($scope.formData.data),
                dataType: $scope.formData.dataType,
                success: function (data) {
                    var json = new JSONFormat(data);
                    $("#codeDiv").html(json.toString()).show();
                    if (!$rootScope.$$phase) {
                        $scope.$apply();
                    }
                    alert("well done!");
                }
            });
        }

        $("#dataTextArea").blur(function () {
            var json = new JSONFormat(JSON.parse($scope.formData.data));
            $("#dataTextAreaCode").show().html(json.toString());
        });
    }


})();
